<template>
	<view class="container">
		<view class="header-box" :style="{ height: winInfo.height + 50 + 'px', paddingTop: winInfo.top + 'px' }">
			<view class="header-info">
				<tui-icon name="arrowleft" size="25" @tap="goBack" color="#000" bold="true"></tui-icon>
				<view class="header-name">{{ headerTitle }}</view>
			</view>
			<view class="search-info">
				<input type="text" v-model="searchKey" placeholder="请输入" maxlength="50" @input="searchInfo" />
				<tui-icon name="close" v-if="searchKey" color="#999" size="20" @tap="clearInfo"></tui-icon>
				<view class="ensure-info" @tap="addSearchInfo">确定</view>
			</view>
		</view>
		<!-- 学校列表 -->
		<view :style="{ paddingTop: winInfo.height + 70 + 'px' }">
			<view class="line-box" v-for="item in DataInfo.Data" @tap="chooseSchool(item)">
				<view>{{ item.Value }}</view>
			</view>
		</view>
	</view>
</template>

<script>
import { replace, filtion } from 'verification-sensitive';
export default {
	data() {
		return {
			Islogin: this.Microi.IsLogin(),
			CurrentUser: this.Microi.GetCurrentUser(),
			winInfo: {
				height: 64, //header高度
				top: 26, //标题图标距离顶部距离
				scrollH: 0 //滚动总高度
			},
			startTime: 0,
			options: {},
			DataInfo: {
				Data: [],
				page: 1,
				noData: false
			},
			searchKey: '',
			headerTitle: ''
		};
	},
	mounted() {
		let obj = {};
		// #ifdef MP-WEIXIN
		obj = wx.getMenuButtonBoundingClientRect();
		// #endif
		// #ifdef MP-BAIDU
		obj = swan.getMenuButtonBoundingClientRect();
		// #endif
		// #ifdef MP-ALIPAY
		my.hideAddToDesktopMenu();
		// #endif

		setTimeout(() => {
			uni.getSystemInfo({
				success: (res) => {
					this.winInfo = res;
					this.winInfo.width = obj.left || res.windowWidth;
					this.winInfo.height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
					this.winInfo.top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
					this.winInfo.scrollH = res.windowWidth;
				}
			});
		}, 0);
	},

	onLoad(options) {
		this.options = options;
		this.headerTitle = options.headerTitle;
		this.getSchoolInfo();
	},
	onShow() {
		this.startTime = new Date();
	},
	onHide() {
		if (this.Islogin) {
			// 获取当前页面栈
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			// 获取当前页面的参数
			const options = currentPage.options;
			this.Microi.ApiEngine.Run('Add_liulanLog', {
				YemianCS: options,
				YemianDZ: route,
				LiulanSJC: new Date().getTime() - this.startTime.getTime()
			});
		}
	},
	onUnload() {
		if (this.Islogin) {
			// 获取当前页面栈
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			// 获取当前页面的参数
			const options = currentPage.options;
			this.Microi.ApiEngine.Run('Add_liulanLog', {
				YemianCS: options,
				YemianDZ: route,
				LiulanSJC: new Date().getTime() - this.startTime.getTime()
			});
		}
	},
	methods: {
		searchInfo() {
			if (this.searchKey) {
				this.DataInfo.page = 1;
				this.DataInfo.noData = false;
				this.DataInfo.Data = [];
				this.getSchoolInfo();
			}
		},
		async getSchoolInfo() {
			var self = this;
			if (this.DataInfo.noData) {
				this.Microi.Tips('暂无更多信息', false);
				return;
			}
			var res = await self.Microi.ApiEngine.Run('get_HDfb_info', {
				Type: self.options.Type,
				searchKey: this.searchKey || '',
				page: this.DataInfo.page
			});

			if (this.DataInfo.page == 1) {
				this.DataInfo.Data = res.Data;
			} else {
				this.DataInfo.Data.push(...res.Data);
			}
			if (!res.Data.length) {
				this.DataInfo.noData = true;
				return;
			}
			this.DataInfo.page++;
		},
		clearInfo() {
			this.searchKey = '';
			this.searchInfo();
		},
		async addSearchInfo() {
			var self = this;
			if (!this.searchKey || filtion(this.searchKey)) {
				this.Microi.Tips('请检查搜索内容', false);
				return;
			}
			var res = await this.Microi.ApiEngine.Run('add_schholwork_searchInfo', {
				Name: this.searchKey,
				TableName: 'Sys_BaseData',
				ParentKey: this.options.ParentKey
			});
			setTimeout(function () {
				self.chooseSchool(res.Data);
			}, 500);
		},
		chooseSchool(item) {
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			var companyInfo = {
				name: item.Value,
				Id: item.Id
			};
			uni.setStorageSync(this.options.InfoName, companyInfo);
			this.goBack();
		},
		goBack() {
			let pages = getCurrentPages(); // 获取当前页面堆栈
			let pageCount = pages.length; // 页面堆栈中的页面数量
			// 判断是否有上级页面
			if (pageCount > 1) {
				// 如果有上级页面，则返回上级页面
				uni.navigateBack({
					delta: 1
				});
			} else {
				// 如果没有上级页面，则返回首页
				uni.switchTab({
					url: '/pages/tabbar/find/find' // 替换为你的首页路径
				});
			}
		}
	},
	onReachBottom() {
		this.getSchoolInfo();
	}
};
</script>

<style>
page {
	background-color: #fff;
}
.container {
	background-color: #fff;
	padding: 0 20rpx 150rpx 20rpx;
	color: #000;
	box-sizing: border-box;
}

.header-box {
	width: 100%;
	position: fixed;
	left: 0;
	top: 0;
	z-index: 99999;
	background-color: #fff;
	/* display: flex;
	align-items: center;
	justify-content: space-between; */
	box-sizing: border-box;
}
.search-info {
	display: flex;
	align-items: center;
	justify-content: flex-start;
	height: 100rpx;
	box-sizing: border-box;
	padding: 16rpx;
	border-bottom: 1rpx solid #f2f2f2;
	gap: 16rpx;
	background-color: #fff;
	position: relative;
}
.search-info input {
	width: 100%;
	height: 68rpx;
	background: #f5f5f5;
	border-radius: 34rpx;
	padding: 0 30rpx;
	font-size: 28rpx;
	position: relative;
	z-index: 999;
}
.ensure-info {
	white-space: nowrap;
}
.line-box {
	padding: 16rpx;
	display: flex;
	align-items: center;
	justify-content: flex-start;
	gap: 16rpx;
	border-bottom: 1rpx solid #f2f2f2;
}
.line-box image {
	width: 60rpx;
	height: 60rpx;
	border-radius: 50%;
}
.header-name {
	font-family: PingFang SC, PingFang SC;
	/* font-weight: 800; */
	font-size: 36rpx;
	color: #181818;
	text-align: center;
	font-style: normal;
	text-transform: none;
}
.header-info {
	display: flex;
	align-items: center;
	justify-content: flex-start;
	box-sizing: border-box;
	gap: 20rpx;
	padding-bottom: 16rpx;
}
.header-info image {
	width: 25rpx;
}
.header-btn {
	display: flex;
	align-items: center;
	justify-content: flex-start;
	gap: 20rpx;
}
.header-btn image {
	width: 44rpx;
	height: 44rpx;
}
.hearder-container {
	padding: 20rpx 0;
	border-bottom: 2rpx solid #f4f5f6;
}

.no-Data {
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
	font-size: 25rpx;
	color: #999999;
	margin: auto;
	margin-top: 200rpx;
}
.no-Data image {
	width: 400rpx;
}
</style>
